package com.hdy.orderforthree.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdy.common.AndroidUtils;
import com.hdy.common.loadimage.LoadImageController;
import com.hdy.net.order.bean.DishBean;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;

import java.util.List;

import butterknife.BindView;

public class BillAdapter extends BaseAdapter {

    static class ViewHolder extends BaseAdapter.ViewHolder{

        @BindView(R2.id.bill_item_sort)
        TextView mSort;
        @BindView(R2.id.bill_item_image)
        ImageView mFoodImage;
        @BindView(R2.id.bill_item_name)
        TextView mFoodName;
        @BindView(R2.id.bill_item_minus)
        ImageView mFoodMinus;
        @BindView(R2.id.bill_item_qty)
        TextView mFoodQty;
        @BindView(R2.id.bill_item_plus)
        ImageView mFoodPlus;
        @BindView(R2.id.bill_item_price)
        TextView mFoodPrice;
        @BindView(R2.id.bill_item_status)
        TextView mState;
        @BindView(R2.id.bill_item_time)
        TextView mTime;

        public ViewHolder(View view){
            super(view);
        }
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        super.setOnItemClickListener(onItemClickListener);
    }

    public BillAdapter(List list) {
        super(R.layout.bill_item, list);
    }

    @Override
    public BaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        RecyclerView.ViewHolder holder = new ViewHolder(view);
        return (ViewHolder) holder;
    }

    @Override
    public void onBindViewHolder(BaseAdapter.ViewHolder holder, int position) {
        DishBean food = (DishBean) mList.get(position);
        BillAdapter.ViewHolder foodHolder = (BillAdapter.ViewHolder) holder;

        foodHolder.mSort.setText(String.valueOf(position + 1));

        int resId = AndroidUtils.getDrawableResourceId(foodHolder.mFoodImage.getContext(),"image" + position);
        LoadImageController.getInstance().loadImage(resId, foodHolder.mFoodImage);
        foodHolder.mFoodName.setText(food.getName());
        foodHolder.mFoodName.setText(food.getName());

        String temp = foodHolder.mFoodPrice.getContext().getResources().getString(R.string.food_price);
        foodHolder.mFoodPrice.setText(String.format(temp, (int) food.getPrice()));

        foodHolder.mFoodPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnPlusClick(v, holder, position, mList.get(position));
                }
            }
        });

        foodHolder.mFoodMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnMinusClick(v, holder, position, mList.get(position));
                }
            }
        });

        foodHolder.mFoodQty.setText(String.valueOf(food.getQty()));
        if (food.getQty() > 0) {
            foodHolder.mFoodQty.setVisibility(View.VISIBLE);
            foodHolder.mFoodMinus.setVisibility(View.VISIBLE);
        } else {
            foodHolder.mFoodQty.setVisibility(View.INVISIBLE);
            foodHolder.mFoodMinus.setVisibility(View.INVISIBLE);
        }

        if (food.isConfirmed()) {
            foodHolder.mState.setBackground(null);
            foodHolder.mState.setText(foodHolder.mState.getContext().getString(R.string.ordered));
        } else {
            foodHolder.mState.setBackground(foodHolder.mState.getContext().getDrawable(R.drawable.delete));
        }
        foodHolder.mState.setTag(position);
        foodHolder.mState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                DishBean dish = (DishBean) mList.get(position);
                if (!dish.isConfirmed()) {
                    mList.remove(dish);
                    mOnItemClickListener.onDeleteClick(v, holder, position, dish);
                }
            }
        });
    }

    /**
     * 数组0返回未下单总金额，1返回已下单总金额
     * @return
     */
    public float[] getMoney() {
        float[] money = new float[2];
        for(Object obj : mList) {
            DishBean dish = (DishBean) obj;
            if (dish.isConfirmed()) {
                money[1] = money[1] + dish.getPrice() * dish.getQty();
            } else {
                money[0] = money[0] + dish.getPrice() * dish.getQty();
            }
        }
        return money;
    }
}
